package com.co.user.controller;

import com.co.common.entity.Result;
import com.co.user.pojo.Later;
import com.co.user.service.LaterService;
import com.co.video.pojo.Detail;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 */

@RestController
@RequestMapping("/user/later")

public class LaterController {

    @Autowired
    private LaterService laterService;

    /**
     * UsLater分页条件搜索实现
     *
     * @param later
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Later>> findPage(@RequestBody(required = false) Later later, @PathVariable int page, @PathVariable int size) {
        //调用UsLaterService实现分页条件查询UsLater
        PageInfo<Later> pageInfo = laterService.findPage(later, page, size);
        return Result.ok(pageInfo);
    }

    /**
     * UsLater分页搜索实现
     *
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Later>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用UsLaterService实现分页查询UsLater
        PageInfo<Later> pageInfo = laterService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param later
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Later>> findList(@RequestBody(required = false) Later later) {
        //调用UsLaterService实现条件查询UsLater
        List<Later> list = laterService.findList(later);
        return Result.ok(list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        //调用UsLaterService实现根据主键删除
        laterService.delete(id);
        return Result.ok();
    }

    /**
     * 清空稍后观看
     *
     * @return
     */
    @DeleteMapping("/deleteAll")
    public Result deleteAll() {
        laterService.deleteAll();
        return Result.ok("稍后观看全部删除");
    }

    /**
     * 修改UsLater数据
     *
     * @param later
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<?> update(@RequestBody Later later, @PathVariable String id) {
        //设置主键值
        later.setId(id);
        //调用UsLaterService实现修改UsLater
        laterService.update(later);
        return Result.ok();
    }

    /**
     * 新增UsLater数据
     *
     * @param later
     * @return
     */
    @PostMapping
    public Result<?> add(@RequestBody Later later) {
        //调用UsLaterService实现添加UsLater
        laterService.add(later);
        return Result.ok();
    }

    /**
     * 根据ID查询UsLater数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Later> findById(@PathVariable String id) {
        //调用UsLaterService实现根据主键查询UsLater
        Later later = laterService.findById(id);
        return Result.ok(later);
    }

    /**
     * 查询UsLater全部数据
     *
     * @return
     */
    @GetMapping
    public Result<List<Later>> findAll() {
        //调用UsLaterService实现查询所有UsLater
        List<Later> list = laterService.findAll();
        return Result.ok(list);
    }

    /**
     * 根据用户名查询用户稍后观看列表
     *
     * @param username
     * @return
     */
    @GetMapping("/user/{username}")
    public Result<Object> findHistoryByUsername(@PathVariable("username") String username) {
        List<Detail> historyByUsername = laterService.findLaterByUsername(username);
        return Result.ok(historyByUsername);
    }
}
